package br.com.bomsabor.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Conversor {
	
	public static double string2money(String str){
		double dbl = 0.00;
		if(str.length()>0) {
			dbl = Double.parseDouble(str
					.replace(".", "")
					.replace(",", "."));
		}
		return dbl;
	}
	public static String money2string(double dbl){
		String str = "0,00";
		if(dbl>0) {
			str = Double.toString(dbl).replace(".",",");
			if (str.substring(str.indexOf(",")+1).length() == -1){
				str += ",00";
			}else if (str.substring(str.indexOf(",")+1).length() == 1){
				str += "0";
			}
		}
		return str;
	}
	public static Date string2date(String str){
		Date d = new Date();
		if(str != null) {
			if(str.length()>0) {
				SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
				try {
					d = sdf.parse(str);
				} catch (ParseException e) {
					//e.printStackTrace();
				}
			}
		}
		return d;
	}
	public static String date2string(Date d){
		String str = "";
		if(d != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			str = sdf.format(d);
		}
		return str;
	}

}
